package gui;

import gamemngr.GameMngr;

import logic.Country;
import logic.moves.ExchangeMove;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.swtdesigner.SWTResourceManager;


public class MoveDialog extends Dialog{
	
	private Text chooseHowManyText;
	protected Shell shell;
	private Country fromCountry;
	private Country toCountry;
	private ExchangeMove move;
	private int numOfSoldiers;
	private GameMngr game;
	private boolean moved;
	private boolean isNetwork;
	
	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public MoveDialog(Shell parent, int style, boolean isNetwork) {
		super(parent, style);
		this.isNetwork = isNetwork;
	}
	
	/**
	 * Create the dialog
	 * @param parent
	 */
	public MoveDialog(Shell parent, boolean isNetwork) {
		this(parent, SWT.NONE, isNetwork);
	}
	
	public MoveDialog(Shell parent, Country FromCountry, Country ToCountry, GameMngr game, boolean isNetwork) {
		this(parent, SWT.NONE, isNetwork);
		this.fromCountry = FromCountry;
		this.toCountry = ToCountry;
		this.game = game;
	}
	
	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		if(moved)
			return move.getBriefDo();
		else
			return null;
	}
	
	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(320, 214);
		shell.setLocation(RiskGUI.getNewPosition(getParent()));
		shell.setText("Move");
		shell.addShellListener(new ShellAdapter() {
			public void shellClosed(final ShellEvent arg0) {
				moved = false;
			}
		});
		moved = false;
		final Label label = new Label(shell, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("", 16, SWT.NONE));
		label.setText("0");
		label.setBounds(245, 85, 24, 34);
		
		final Button moveButton = new Button(shell, SWT.NONE);
		moveButton.setEnabled(false); 
		moveButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				if(numOfSoldiers==0) shell.dispose();
				else {
					move = new ExchangeMove(fromCountry,toCountry,numOfSoldiers);
					game.getCurrentPlayer().addMove(move, isNetwork);
					moved = true;
					shell.dispose();
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}	
		});
		moveButton.setText("Move");
		moveButton.setBounds(115, 130, 59, 23);
		
		final Scale scale = new Scale(shell, SWT.NONE);
		scale.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {				
			numOfSoldiers = scale.getSelection();
			moveButton.setEnabled(numOfSoldiers > 0);
			
			label.setText(Integer.toString(numOfSoldiers));
			}
		});
		scale.setBounds(60, 75, 170, 42);
		int max = fromCountry.getSoldiers() - 1;
		scale.setMaximum(max);
		scale.setMinimum(0);
		scale.setEnabled(true);

		chooseHowManyText = new Text(shell, SWT.WRAP | SWT.MULTI);
		chooseHowManyText.setEditable(false);
		chooseHowManyText.setText("Choose how many soldiers you want to move from " + fromCountry.getName() + " to " + toCountry.getName());
		chooseHowManyText.setBounds(45, 20, 220, 42);
	}

    
}
